package com.example.jianzhioffer;

/**
 * Created by Quincy on 2018/9/10.
 * 输入一个链表，输出该链表中倒数第k个结点。
 */
public class ListKNode {

    public class ListNode{
        int val;
        ListNode next = null;
        ListNode(int val){
            this.val = val;
        }
    }

    public ListNode FindKthToTail(ListNode head,int k){
        ListNode currentNode = head;
        if (k <= 0) return null;
        int count = 0;
        while (currentNode != null){
            count++ ;
            currentNode = currentNode.next;
        }
        if (k > count) return null;
        ListNode currentNode1 = head;
        for (int i = 0; i < count - k; i++){
            currentNode1 = currentNode1.next;
        }
        return currentNode1;
    }
}
